package l.com.tableview;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static final int DEFAULT = 0;
    private static final int HEAD = 1;
    TableView tableView;
    int rowCount;
    int columnCount;
    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tableView = (TableView) findViewById(R.id.table);
        rowCount = 100;
        columnCount = 6;
        tableView.setAdapter(new MyAdapter(this));
        //      tableView.postDelayed(new Runnable() {
        //            @Override
        //            public void run() {
        //                rowCount = 10;
        //                columnCount = 10;
        //                tableView.setAdapter(new MyAdapter(MainActivity.this));
        //            }
        //        },4000);

    }

    class MyAdapter implements TableAdapter {
        LayoutInflater inflater;
        Context ct;

        public MyAdapter(Context ct) {
            this.ct = ct;
            inflater = LayoutInflater.from(ct);
        }

        @Override
        public int getHeight(int row) {
            return 40;
        }

        @Override
        public int getWidth(int column) {
            return 120;
        }

        @Override
        public int getRowCount() {
            return rowCount;
        }

        @Override
        public int getColumnCount() {
            return columnCount;
        }

        @Override
        public View getView(int row, int column, View convertView, ViewGroup parent) {
            int layoutId = 0;
            switch (getItemType(row, column)) {
                case DEFAULT:
                    layoutId = R.layout.default_item;
                    break;

                case HEAD:
                    layoutId = R.layout.head_item;
                    break;
            }
            if (convertView == null) {
                convertView = inflater.inflate(layoutId, null);
            }
            TextView tv = convertView.findViewById(R.id.tv);
            if (row == 0 && column == 0) {
                tv.setText("-----");
            } else if (row == 0) {
                tv.setText("第" + column + "列");
            } else if (column == 0) {
                tv.setText("第" + row + "行");
            } else {
                tv.setText(row + "行," + column + "列");
            }


            return convertView;
        }

        @Override
        public int getItemType(int row, int column) {
            if (row == 0 || column == 0) {
                return HEAD;
            }
            return DEFAULT;
        }

        @Override
        public int getTypeCount() {
            return 2;
        }


    }

}
